package com.telenav.framework.persistent;


public class PersistentManager {
	
    public final static int DATABASE = 1;

	private static int initCount;
	
	private static PersistentManager persistentManager;

    private static PersistentContext persistentContext;
    
    public static PersistentManager getInstance()
    {
        return persistentManager;
    }
    
    public synchronized static void init(PersistentManager persistentMngr, PersistentContext persistentCtxt)
    {
        if(initCount >= 1)
            return;
        
        persistentManager = persistentMngr;
        persistentContext = persistentCtxt;
        initCount++;
    }
    
    

    /**
     * Get current persistent context object.
     * 
     * @return {@link ITnPersistentContext}
     */
    public final PersistentContext getPersistentContext()
    {
        return persistentContext;
    }

    /**
     * create a store object according to the store type and name.
     * 
     * @param storeType using {@link #RMS_STORE}, {@link #DATABASE_STORE}, {@link #SDCARD_STORE} etc.
     * @param name the name of store object.
     * @return {@link TnStore}
     */
    public final AbstractStore createStore(int storeType, String name)
    {
        if (storeType < 0 || storeType > 2)
        {
            throw new IllegalArgumentException("Doesn't support this store type currently.");
        }
        else if(name == null || name.trim().length() == 0)
        {
            throw new IllegalArgumentException("The store name is empty.");
        }
        
        return createStoreDelegate(storeType, name);
    }
    
    
    protected AbstractStore createStoreDelegate(int storeType, String name)
    {
    	AbstractStore store = null;
        
        switch(storeType)
        {
            case DATABASE:
            {
                store = new DatabaseStore(name, storeType);
                break;
            }
        }
        
        return store;
    }
    
}
